﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;

namespace posttracker.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void LoginingUser(object sender, EventArgs e)
        {
            
            string password = "";
            using (SqlConnection conn = new SqlConnection("Data Source=G74-PC\\SQLEXPRESS;Initial Catalog=posttrackerApi2.Models.posttracker_db;Integrated Security=True"))
            {
                conn.Open();
                SqlCommand command = new SqlCommand("Select UserName, Password From Users where UserName ='" + LoginUser.UserName + @"'", conn);
                
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    password = (string)reader["Password"];
                }
                conn.Close();

                
            }
            //LoginUser.UserName
            if (password == LoginUser.Password)
            {
                FormsAuthentication.SetAuthCookie(LoginUser.UserName, false /* createPersistentCookie */);
                LoginUser.InstructionText = String.Empty;

                string continueUrl = "~/";

                Response.Redirect(continueUrl);
                
            }



        }
    }
}
